﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:converters="clr-namespace:MahApps.Metro.Converters"
                    xmlns:Controls="clr-namespace:MahApps.Metro.Controls">

    <converters:PanoramaGroupWidthConverter x:Key="conv" />
    <converters:PanoramaGroupHeightConverter x:Key="PanoramaGroupHeightConverter" />
    <converters:PanoramaGroupHeaderConverter x:Key="PanoramaGroupHeaderConverter" />

    <Style x:Key="headerLabelStyle" TargetType="Label">
        <Setter Property="FontSize" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:Panorama}, Mode=FindAncestor}, Path=HeaderFontSize}" />
        <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:Panorama}, Mode=FindAncestor}, Path=HeaderFontColor}" />
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding}" Foreground="{Binding RelativeSource={RelativeSource AncestorType=Label}, Path=Foreground}" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="FontFamily" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:Panorama}, Mode=FindAncestor}, Path=HeaderFontFamily}" />
        <Setter Property="FontWeight" Value="Light" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Top" />
        <Setter Property="VerticalContentAlignment" Value="Top" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:Panorama}},Path=DynamicGroupHeader}" Value="False">
                <Setter Property="Margin" Value="10,0,0,20" />
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:Panorama}},Path=DynamicGroupHeader}" Value="True">
                <Setter Property="Margin">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource PanoramaGroupHeaderConverter}">
                            <Binding RelativeSource="{RelativeSource Self}" />
                            <Binding RelativeSource="{RelativeSource AncestorType=ScrollViewer}" Path="ContentHorizontalOffset" />
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <DataTemplate DataType="{x:Type Controls:PanoramaGroup}">
        <DataTemplate.Resources>
            <Style x:Key="transparentListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
                <Style.Resources>
                    <!--<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />-->
                </Style.Resources>
                <Setter Property="Padding" Value="0" />
                <Setter Property="Margin" Value="0" />
            </Style>
        </DataTemplate.Resources>

        <DockPanel LastChildFill="True" Background="Transparent">
            <Label Style="{StaticResource headerLabelStyle}"
                   Content="{Binding Header}"
                   x:Name="header"
                   DockPanel.Dock="Top" />
            <ListBox ItemsSource="{Binding Tiles}" 
                     x:Name="items"
                     SelectionMode="Single"
                     BorderThickness="0"
                     BorderBrush="Transparent"
                     Background="Transparent"
                     VerticalAlignment="Top"
                     IsSynchronizedWithCurrentItem="True"
                     ItemContainerStyle="{StaticResource transparentListBoxItemStyle}">

                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel>
                            <WrapPanel.Height>
                                <MultiBinding Converter="{StaticResource PanoramaGroupHeightConverter}">
                                    <Binding Path="ItemBox" RelativeSource="{RelativeSource AncestorType={x:Type Controls:Panorama}, Mode=FindAncestor}" />
                                    <Binding Path="ActualHeight" RelativeSource="{RelativeSource AncestorType={x:Type Controls:Panorama}, Mode=FindAncestor}" />
                                    <Binding Path="ActualHeight" ElementName="header"/>
                                </MultiBinding>
                            </WrapPanel.Height>
                            <WrapPanel.Width>
                                <MultiBinding Converter="{StaticResource conv}">
                                    <Binding Path="ItemBox" RelativeSource="{RelativeSource AncestorType={x:Type Controls:Panorama}, Mode=FindAncestor}" />
                                    <Binding Path="ActualHeight" ElementName="items"/>
                                    <Binding RelativeSource="{RelativeSource AncestorType={x:Type ListBox}, Mode=FindAncestor}" />
                                    <Binding Path="Items.Count" ElementName="items"/>
                                </MultiBinding>
                            </WrapPanel.Width>
                        </WrapPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>

            </ListBox>
        </DockPanel>
    </DataTemplate>

    <Style TargetType="{x:Type Controls:Panorama}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ItemsControl}">
                    <ControlTemplate.Resources>
                        <Style TargetType="{x:Type ScrollViewer}">
                            <Setter Property="HorizontalScrollBarVisibility" Value="Hidden" />
                            <Setter Property="VerticalScrollBarVisibility" Value="Hidden" />
                        </Style>
                    </ControlTemplate.Resources>

                    <ScrollViewer x:Name="PART_ScrollViewer" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                        <ItemsPresenter Margin="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
